package com.clinic.dao.imp;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.clinic.dao.interfaces.Secretary_dao;
import com.clinic.domain.Secretary;
import com.clinic.util.Util_imp;

public class Secretary_imp implements Secretary_dao {

	SessionFactory session_factory = null;

	public Secretary_imp() {
		session_factory = Util_imp.get_sessionfactory();
	}

	@Override
	public int create_secretary(Secretary s) {
		System.out.println("Enter - create_secretary");
		Session session = session_factory.openSession();
		Transaction tx = null;
		int sy_obj_id = 0;
		try {
			tx = session.beginTransaction();
			sy_obj_id = (Integer) session.save(s);
			tx.commit();
			System.out.println("New Secretary Created with ID = " + sy_obj_id);
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Create Secretary Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - create_secretary");
		return sy_obj_id;
	}

	@Override
	public void update_secretary(Secretary sy) {
		System.out.println("Enter - update_secretary");
		Session session = session_factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(sy);
			tx.commit();
			System.out.println("Secretary with ID: " + sy.getSy_obj_id() + " Updated");
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Update Secretary Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - update_secretary");
	}

	@Override
	public void delete_secretary(int sy_obj_id) {
		System.out.println("Enter - delete_secretary");
		Session session = session_factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Secretary s = (Secretary) session.get(Secretary.class, sy_obj_id);
			session.delete(s);
			tx.commit();
			System.out.println("Secretary with ID: " + sy_obj_id + " Deleted");
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Delete Secretary Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - delete_secretary");
	}

	@Override
	public Secretary get_secretary(int sy_obj_id) {
		System.out.println("Enter - get_secretary");
		Session session = session_factory.openSession();
		Transaction tx = null;
		Secretary s = null;
		try {
			tx = session.beginTransaction();
			s = (Secretary) session.createQuery("from Secretary where sy_obj_id=:id")
					.setParameter("id", sy_obj_id).uniqueResult();
			tx.commit();
			System.out.println("Returned Object Full Name: " + s.getFull_name());
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - get_secretary");
		return s;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Secretary> get_all_secretaries() {
		System.out.println("Enter - get_all_secretaries");
		Session session = session_factory.openSession();
		Transaction tx = null;
		List<Secretary> l = null;
		try {
			tx = session.beginTransaction();
			l = session.createQuery("from Secretary").list();
			tx.commit();
			System.out.println("Number of Secretary returned is: " + l.size());
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now get All Secretary Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - get_all_secretaries");
		return l;
	}

	@Override
	public Secretary persistOrMerge(Secretary s) {
		return (Secretary) session_factory.getCurrentSession().merge(s);
	}

	@Override
	public int getsize() {
		System.out.println("Enter - getsize");
		Session session = session_factory.openSession();
		int size = 0;
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			size = ((Long) session
					.createQuery("select count(*) from Secretary")
					.uniqueResult()).intValue();
			tx.commit();
			System.out.println("Count(*) Secretary: " + size);
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - getsize");
		return size;
	}
}